$(function () {
    //变量抽取
    var collect_dataGrid = $("#collect_dataGrid");
    var collect_form = $("#collect_form");
    var collect_dialog = $("#collect_dialog");

    /* 收费查询 数据表格 */
    collect_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/billRecord/query.do",
        fit: true,
        columns: [[
            {field: 'sn', title: '保单号', width: 100},
            {field: 'holder', title: '投保人', width: 100,formatter:function (value) {
                    return value?value.clientName:"";
                }},
            {field: 'holder2', title: '电话', width: 100,formatter:function (value,row) {
                    return row?row.holder.tel:"";
                }},
            {field: 'holder3', title: '客户地址', width: 100,formatter:function (value,row) {
                    return row?row.holder.clientAddress:"";
                }},
            {field: 'holder4', title: '身份证号', width: 100,formatter:function (value,row) {
                    return row?row.holder.idCard:"";
                }},
            {field: 'totalAmount', title: '保费金额', width: 100},
            {field: 'state', title: '缴费状态', width: 100},
            {field: 'payWay', title: '缴费方式', width: 100,formatter: function (value) {
                    var name;
                    $.ajax({
                        type: "POST",
                        url: "/dictionary/selectDictionaryItemById.do",
                        async: false,
                        data: {id: value},
                        success: function (msg) {
                            name = msg.name;
                        }
                    });
                    return name;
                }},
            {field: 'beginDate', title: '起保日期', width: 100},
            {field: 'ddate', title: '止保日期', width: 100,formatter:function (value) {
                    return new Date(value).toLocaleDateString();
                }},
            {field: 'inputUser', title: '录入人', width: 100,formatter:function (value) {
                    return value?value.realname:"";
                }}
        ]],
        queryParams: {
            state:"缴费.."
        },
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    });

    /* 查看信息弹框 */
    collect_dialog.dialog({
        width: 280,
        height: 340,
        buttons: "#btns",
        closed: true,
        resizable:true,
        onClose: function () {
            collect_form.form("clear");
        }
    });

    var methodObj = {
        /*查询*/
        search: function () {
            var keyword = $("[name=keyword]").val();
            var companyId = $("[name=companyId]").val();

            collect_dataGrid.datagrid('load', {
                keyword: keyword,
                companyId:companyId
            });
        },
        /*刷新*/
        reload:function () {
            collect_dialog.dialog("load");
        },
        /*查看信息*/
        check:function () {
            var row = collect_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中编辑数据', 'info');
                return;
            }
            collect_form.form("load", row);
            var endDate= new Date(row.ddate).toLocaleDateString();
            $("#endDate").textbox("setValue",endDate);
            $("#clientName").textbox("setValue",row.holder.clientName);
            $("#idCard").textbox("setValue",row.holder.idCard);
            $("#inputUser").textbox("setValue",row.inputUser.realname);
            collect_dialog.dialog("setTitle","保单信息");
            collect_dialog.dialog("open");
        },
        /*取消*/
        cancel: function () {
            collect_dialog.dialog("close");
        }

    };

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
})
